<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Theme_model extends CI_Model {

	private $_tblCustomTheme = 'custom_theme';
	private $_tblTheme = 'themes';

	public function __construct() {
		parent::__construct();
		$this->load->database();
	}

	function insertTheme($dataThemes) {
		$sql = "INSERT INTO `themes`(`name`, `description`, `picture_theme`, `file_name`, `path_theme`) VALUES (?,?,?,?,?)";
    	$stmt = $this->db->prepare($sql);
    	$stmt->bindParam(1, $dataThemes['name']);
    	$stmt->bindParam(2, $dataThemes['description']);
    	$stmt->bindParam(3, $dataThemes['picture_theme']);
    	$stmt->bindParam(4, $dataThemes['file_name']);
    	$stmt->bindParam(5, $dataThemes['path_theme']);
	    $stmt->execute();
	    $stmt->closeCursor();
	}

	function insertCustomTheme($dataCustomTheme) {
		if($this->getOneCustomTheme($dataCustomTheme['survey_id'], $dataCustomTheme['theme_id'], $dataCustomTheme['user_id']) == NULL) {
			$sql = "INSERT INTO `custom_theme`(survey_id, theme_id, user_id) VALUES (?,?,?)";
	    	$stmt = $this->db->prepare($sql);
	    	$stmt->bindParam(1, $dataCustomTheme['survey_id']);
	    	$stmt->bindParam(2, $dataCustomTheme['theme_id']);
	    	$stmt->bindParam(3, $dataCustomTheme['user_id']);
		    $stmt->execute();
		}
	}

	function insertCustomColorsTheme($dataCustomTheme) {
		if($this->getOneCustomTheme($dataCustomTheme['survey_id'], $dataCustomTheme['theme_id'], $dataCustomTheme['user_id']) != NULL) {

			$this->updateCustomColorsTheme($dataCustomTheme);
		}else {
			$sql = "INSERT INTO `custom_theme`(survey_id, theme_id, user_id, bg_body, page_bg_color, page_title_color, page_text_color, header_bg_color, header_text_color, question_bg_color, question_title_color, progress_bar_color) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
	    	$stmt = $this->db->prepare($sql);
	    	$stmt->bindParam(1, $dataCustomTheme['survey_id']);
	    	$stmt->bindParam(2, $dataCustomTheme['theme_id']);
	    	$stmt->bindParam(3, $dataCustomTheme['user_id']);
	    	$stmt->bindParam(4, $dataCustomTheme['bg_body']);
	    	$stmt->bindParam(5, $dataCustomTheme['page_bg_color']);
	    	$stmt->bindParam(6, $dataCustomTheme['page_title_color']);
	    	$stmt->bindParam(7, $dataCustomTheme['page_text_color']);
	    	$stmt->bindParam(8, $dataCustomTheme['header_bg_color']);
	    	$stmt->bindParam(9, $dataCustomTheme['header_text_color']);
	    	$stmt->bindParam(10, $dataCustomTheme['question_bg_color']);
	    	$stmt->bindParam(11, $dataCustomTheme['question_title_color']);
	    	$stmt->bindParam(12, $dataCustomTheme['progress_bar_color']);
		    $stmt->execute();
		}
	}

	function insertCustomOptionsTheme($dataCustomTheme) {
		if($this->getOneCustomTheme($dataCustomTheme['survey_id'], $dataCustomTheme['theme_id'], $dataCustomTheme['user_id']) != NULL) {
			$this->updateCustomOptionsTheme($dataCustomTheme);
		}else {
			$sql = "INSERT INTO `custom_theme`(survey_id, theme_id, user_id, base_font, font_style, width_option, px, auto_width, survey_title, page_title, page_description, question_numbers, back_button, progress_bar, powered_by) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
	    	$stmt = $this->db->prepare($sql);
	    	$stmt->bindParam(1, $dataCustomTheme['survey_id']);
	    	$stmt->bindParam(2, $dataCustomTheme['theme_id']);
	    	$stmt->bindParam(3, $dataCustomTheme['user_id']);
	    	$stmt->bindParam(4, $dataCustomTheme['base_font']);
	    	$stmt->bindParam(5, $dataCustomTheme['font_style']);
	    	$stmt->bindParam(6, $dataCustomTheme['width_option']);
	    	$stmt->bindParam(7, $dataCustomTheme['px']);
	    	$stmt->bindParam(8, $dataCustomTheme['auto_width']);
	    	$stmt->bindParam(9, $dataCustomTheme['survey_title']);
	    	$stmt->bindParam(10, $dataCustomTheme['page_title']);
	    	$stmt->bindParam(11, $dataCustomTheme['page_description']);
	    	$stmt->bindParam(12, $dataCustomTheme['question_numbers']);
	    	$stmt->bindParam(13, $dataCustomTheme['back_button']);
	    	$stmt->bindParam(14, $dataCustomTheme['progress_bar']);
	    	$stmt->bindParam(15, $dataCustomTheme['powered_by']);
		    $stmt->execute();
		}
	}

	function getOneCustomTheme($surveyId, $themeId, $userId) {

		$sql = "SELECT * FROM custom_theme WHERE survey_id=".$surveyId." AND theme_id=".$themeId." AND user_id=".$userId;
    	$stmt = $this->db->prepare($sql);
	    $stmt->execute();
	    $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
	    return $rs;
	}

	function updateCustomColorsTheme($dataCustomTheme) {
		$sql = "UPDATE `custom_theme` SET bg_body=?, page_bg_color=?, page_title_color=?, page_text_color=?, header_bg_color=?, header_text_color=?, question_bg_color=?, question_title_color=?, progress_bar_color=? WHERE survey_id=".$dataCustomTheme['survey_id']." AND theme_id=".$dataCustomTheme['theme_id']." AND user_id=".$dataCustomTheme['user_id'];
	    	$stmt = $this->db->prepare($sql);
	    	$stmt->bindParam(1, $dataCustomTheme['bg_body']);
	    	$stmt->bindParam(2, $dataCustomTheme['page_bg_color']);
	    	$stmt->bindParam(3, $dataCustomTheme['page_title_color']);
	    	$stmt->bindParam(4, $dataCustomTheme['page_text_color']);
	    	$stmt->bindParam(5, $dataCustomTheme['header_bg_color']);
	    	$stmt->bindParam(6, $dataCustomTheme['header_text_color']);
	    	$stmt->bindParam(7, $dataCustomTheme['question_bg_color']);
	    	$stmt->bindParam(8, $dataCustomTheme['question_title_color']);
	    	$stmt->bindParam(9, $dataCustomTheme['progress_bar_color']);
		    $stmt->execute();
	}

	function updateCustomOptionsTheme($dataCustomTheme) {
		$sql = "UPDATE `custom_theme` SET base_font='".$dataCustomTheme['base_font']."', font_style='".$dataCustomTheme['font_style']."', width_option='".$dataCustomTheme['width_option']."', px='".$dataCustomTheme['px']."', auto_width='".$dataCustomTheme['auto_width']."', survey_title='".$dataCustomTheme['survey_title']."', page_title='".$dataCustomTheme['page_title']."', page_description='".$dataCustomTheme['page_description']."', question_numbers='".$dataCustomTheme['question_numbers']."', back_button='".$dataCustomTheme['back_button']."', progress_bar='".$dataCustomTheme['progress_bar']."', powered_by='".$dataCustomTheme['powered_by']."' WHERE survey_id=".$dataCustomTheme['survey_id']." AND theme_id=".$dataCustomTheme['theme_id']." AND user_id=".$dataCustomTheme['user_id'];

	    	$stmt = $this->db->prepare($sql);
		    $stmt->execute();
	}

	function getOneTheme($idTheme) {
		$sql = "SELECT * FROM themes WHERE id=".$idTheme;
    	$stmt = $this->db->prepare($sql);
	    $stmt->execute();
	    $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
	    return $rs;
	}

	function getAllTheme() {
		$sql = "SELECT * FROM  `themes`";
    	$stmt = $this->db->prepare($sql);
	    $stmt->execute();
	    $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
	    return $rs;
	}
}

?>